package cn.tx.sboot.ioc;/*
 *
 * @author 陈粒
 * @version 创建时间：2021/8/13 14:50
 */

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestIoC {
    //Hello类
    @Test
    public void bean() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");//创建 Spring 的 IOC 容器
        Hello h1 = new Hello();
        System.out.println(h1);
        h1.hi();
        //根据id获取bean
        //Spring就是一个大工厂（容器）专门生成bean,bean就是对象
        Hello hello = (Hello) ac.getBean("hhhh");//创建对象，bean的id是hhhh,也就是beanname，ac.getBean("hhhh")是Object类型
        System.out.println(hello);
        hello.hi();
    }

    //User类，使用@Component
    @Test
    public void get() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");//创建 Spring 的 IOC 容器
        User us = (User) ac.getBean("user");//创建对象，根据id获取bean,beanname就是类名,首字母变小写，ac.getBean("user")是Object类型
        System.out.println(us);
        us.get();
    }
    @Test
    public void TestController() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        TestC tc = (TestC) ac.getBean("testC");
        System.out.println(tc);
        tc.Test();
    }
}